package com.ksd.pug.controller.menu;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ksd.pug.commons.enums.ResultStatusEnumA;
import com.ksd.pug.commons.ex.PugBussinessException;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.controller.BaseController;
import com.ksd.pug.pojo.NavMenu;
import com.ksd.pug.service.menu.INavMenuService;
import com.ksd.pug.vo.MenuVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hcl
 * @date 2022/1/15$ 23:05$
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class NavMenuController extends BaseController {

    private final INavMenuService slideMenuService;

    /**
     * 查询菜单的接口信息-tree
     *
     * @return
     */
    @GetMapping("/navmenu/tree")
    @ResponseBody
    public List<NavMenu> tree() {
        return slideMenuService.findNavMenuTree();
    }

    /**
     * 查询菜单信息并分页
     *
     * @return
     */
    @PostMapping("/navmenu/list")
    public IPage<NavMenu> findNavMenuPage(@RequestBody MenuVo menuVo) {
        return slideMenuService.findNavMenuPage(menuVo);
    }

    /**
     * 保存和修改菜单
     *
     * @return
     */
    @PostMapping("/navmenu/save")
    public NavMenu saveMenu(@RequestBody NavMenu navMenu) {
        boolean flag = slideMenuService.saveOrUpdate(navMenu);
        return flag ? navMenu : null;
    }

    /**
     * 根据id删除菜单
     *
     * @return
     */
    @PostMapping("/navmenu/del/{id}")
    public boolean saveMenu(@PathVariable("id") Long id) {
        NavMenu navMenu = new NavMenu();
        navMenu.setId(id);
        navMenu.setIsdelete(1);
        return slideMenuService.updateById(navMenu);
    }

    /**
     * 根据文章ids批量删除菜单
     *
     * @param menuVO
     * @return
     */
    @PostMapping("/navmenu/delBatch")
    public boolean delMenu(@RequestBody MenuVo menuVO) {
        log.info("你要批量删除的IDS是:{}", menuVO.getBatchIds());
        if (Vsserts.isEmpty(menuVO.getBatchIds())) {
            throw new PugBussinessException(ResultStatusEnumA.EMPTY_IDS_VALID);
        }
        return slideMenuService.delBatchNavMenu(menuVO.getBatchIds());
    }


    /**
     * 根据id查询菜单明细
     *
     * @return
     */
    @GetMapping("/navmenu/get/{id}")
    public NavMenu getMenu(@PathVariable("id") Long id) {
        return slideMenuService.getById(id);
    }
}
